﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.OleDb;
using DTO;
using DAO;
using BUS;
namespace QL_xxx
{
    /// <summary>
    /// Interaction logic for QLPhong_View.xaml
    /// </summary>
    public partial class QLPhong_View : UserControl
    {
        private const string phongCoKhach = "RoomWithGuest.png";
        private const string phongTrong = "NormalRoom.png";

        private List<PhongDTO> listPhong;
        private btnPhong[,] listIconPhong;
        private int row = 0;//toa do chuot theo o trong grid DSphong
        private int col = 0;
        public QLPhong_View()
        {
            this.InitializeComponent();
            listIconPhong = new btnPhong[5, 10];    
        }
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            listPhong = PhongBUS.LayDSPhong();
            LoadDSPhong(listPhong);
        }
        private void LoadDSPhong(List<PhongDTO> _listPhong)
        {
            GridDSPhong.Children.Clear();
            if (_listPhong != null && _listPhong.Count > 0)
            {
                for (int i = 0; i < _listPhong.Count; i++)
                {
                    if (listPhong[i].TrangThai == 0)//phong trong
                    {
                        btnPhong p = new btnPhong(listPhong[i].TenPhong, phongTrong);
                        listIconPhong[listPhong[i].Dong, listPhong[i].Cot] = p;
                        AddToGrid(p, listPhong[i].Dong, listPhong[i].Cot);
                    }
                    else
                        if (listPhong[i].TrangThai != 0)//phong co khach
                        {
                            btnPhong p = new btnPhong(listPhong[i].TenPhong, phongCoKhach);
                            listIconPhong[listPhong[i].Dong, listPhong[i].Cot] = p;
                            AddToGrid(p, listPhong[i].Dong, listPhong[i].Cot);
                        }
                    
                }
            }
        }
        private void GridDSPhong_Drop(object sender, DragEventArgs e)
        {
            try
            {
                e.Effects = DragDropEffects.None;

                #region tim vi tri theo grid
                int x = (int)e.GetPosition(GridDSPhong).X;
                int y = (int)e.GetPosition(GridDSPhong).Y;
                //
                int sizeOfOne_r = (int)GridDSPhong.ActualHeight / GridDSPhong.RowDefinitions.Count;
                double posTemp_r = 1.0 * y / sizeOfOne_r;
                row = (posTemp_r - (int)posTemp_r) > 0 ? (int)posTemp_r : (int)posTemp_r - 1;
                //
                int sizeOfOne_c = (int)GridDSPhong.ActualWidth / GridDSPhong.ColumnDefinitions.Count;
                double posTemp_c = 1.0 * x / sizeOfOne_c;
                col = (posTemp_c - (int)posTemp_c) > 0 ? (int)posTemp_c : (int)posTemp_c - 1;
                #endregion

                #region them btn vao grid
                var coppy = ((btnPhong)e.Data.GetData(typeof(btnPhong))).Clone();
                int oldRow = Grid.GetRow(((btnPhong)e.Data.GetData(typeof(btnPhong))));
                int oldCol = Grid.GetColumn(((btnPhong)e.Data.GetData(typeof(btnPhong))));
                if ((oldCol == col && oldRow == row) || listIconPhong[row, col] != null)// neu vi tri moi giong vi tri cu , hay vi tri moi da co phong => khong lam
                    return;
                //
                //lay phong duoc thay doi vi tri
                PhongDTO phong = PhongBUS.LayDSPhong(-1, coppy.chu.Text)[0];
                //sua thong tin dong va cot
                phong.Dong = row;
                phong.Cot = col;
                //luu lai
                bool kq = PhongBUS.SuaViTri(phong);
                if (!kq)
                {
                    MessageBox.Show("Lỗi chưa xác định, vui lòng liên hệ nhà cung cấp!");
                    return;
                }
                //sua vi tri trong giao dien
                listIconPhong[row, col] = coppy;
                AddToGrid(listIconPhong[row, col], row, col);
                GridDSPhong.Children.Remove(listIconPhong[oldRow, oldCol]);
                listIconPhong[oldRow, oldCol] = null;
                #endregion
            }
            catch
            {
            }
        }
        void QLPhong_View_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            var dependencyObject = (btnPhong)sender;
            DragDrop.DoDragDrop(dependencyObject, dependencyObject, DragDropEffects.Move);
        }
        private void AddToGrid(btnPhong phong, int _row, int _col)
        {
            if (phong != null)
            {
                phong.Padding = new Thickness(10, 5, 10, 5);
                phong.SetValue(Grid.RowProperty, _row);
                phong.SetValue(Grid.ColumnProperty, _col);
                phong.PreviewMouseDown += new MouseButtonEventHandler(QLPhong_View_PreviewMouseDown);
                phong.MouseDoubleClick += new MouseButtonEventHandler(phong_MouseDoubleClick);
                phong.MouseRightButtonDown += new MouseButtonEventHandler(phong_MouseRightButtonDown);
                GridDSPhong.Children.Add(phong);
            }
        }

        void phong_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            MenuSP.IsOpen = true;
            
        }

        void phong_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ThongTinPhong tt = new ThongTinPhong((sender as btnPhong).chu.Text);
            tt.onSuaPhongThanhCong += new OnSuaPhongThanhCong(tt_onSuaPhongThanhCong);
            tt.onLapPhieuThueThanhCong += new OnLapPhieuThuePhongThanhCong(tt_onLapPhieuThueThanhCong);
            tt.onThanhToanThanhCong += new OnThanhToanThanhCong(tt_onThanhToanThanhCong);
            tt.onXoaPhongThanhCong += new OnXoaPhongThanhCong(tt_onXoaPhongThanhCong);
            tt.ShowDialog();
        }

        void tt_onXoaPhongThanhCong()
        {
            listPhong = PhongBUS.LayDSPhong();
            LoadDSPhong(listPhong);
        }

        void tt_onThanhToanThanhCong()
        {
            listPhong = PhongBUS.LayDSPhong();
            LoadDSPhong(listPhong);
        }

        void tt_onLapPhieuThueThanhCong()
        {
            listPhong = PhongBUS.LayDSPhong();
            LoadDSPhong(listPhong);
        }

        void tt_onSuaPhongThanhCong()
        {
            listPhong = PhongBUS.LayDSPhong();
            LoadDSPhong(listPhong);
        }

        private void btnThem_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	ThemPhong themPhong = new ThemPhong();
            themPhong.onThemPhongThanhCong += new OnThemPhongThanhCong(themPhong_onThemPhongThanhCong);
			themPhong.ShowDialog();
            
        }

        void themPhong_onThemPhongThanhCong()
        {
            listPhong = PhongBUS.LayDSPhong();
            LoadDSPhong(listPhong);
        }

        
    }


}
